/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera.ui;

import android.content.Context;
import android.view.View;

import com.android.camera.CameraPreference.OnPreferenceChangedListener;
import com.android.camera.IconListPreference;
import com.android.camera.ListPreference;
import com.android.camera.R;

/**
 * A view for switching the FlashMode.
 */
public class FlashPicker extends RotateImageView implements View.OnClickListener {

    private OnPreferenceChangedListener mListener;

    private ListPreference mPreference;

    private CharSequence[] mFlashValues;

    private String mFlashMode;

    private int[] mIconIds = null;

    private int mFlashIndex = 0;

    public FlashPicker(Context context) {
        super(context);
        // setImageResource(mImageResource);
        setContentDescription(getResources().getString(R.string.accessibility_camera_picker));
    }

    public void setListener(OnPreferenceChangedListener listener) {
        mListener = listener;
    }

    public void initialize(ListPreference pref) {
        mPreference = pref;
        mFlashValues = pref.getEntryValues();
        if (mFlashValues == null)
            return;
        setOnClickListener(this);
        mFlashMode = pref.getValue();
        setVisibility(View.VISIBLE);

        mIconIds = ((IconListPreference) pref).getIconIds();

        for (int i = 0; i < mFlashValues.length; i++) {
            if (mFlashValues[i].equals(mFlashMode)) {
                mFlashIndex = i;
            }
        }
        setImageResource(mIconIds[mFlashIndex]);
    }

    @Override
    public void onClick(View v) {

        if (mFlashIndex < mFlashValues.length - 1) {
            mFlashIndex = mFlashIndex + 1;
        } else {
            mFlashIndex = 0;
        }
        mPreference.setValueIndex(mFlashIndex);
        setImageResource(mIconIds[mFlashIndex]);
        mListener.onSharedPreferenceChanged();
    }
}
